package cn.gdcp.broadcast;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity  implements TimeInterface{ ;
             private NetworkchangeReceiver networkchangeReceiver;
             private TextView time_text;
             private TimeBroadcast timeBroadcast;
             @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);






        initBunder();
        IntentFilter initentFilter = new IntentFilter();
        initentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
         networkchangeReceiver = new NetworkchangeReceiver();
         registerReceiver(networkchangeReceiver,initentFilter);

                 timeBroadcast=new TimeBroadcast(MainActivity.this);
                 IntentFilter intentFilterTime=new IntentFilter();
                 intentFilterTime.addAction(Intent.ACTION_TIME_TICK);
                 intentFilterTime.addAction(Intent.ACTION_TIME_CHANGED);
                 intentFilterTime.addAction(Intent.ACTION_TIMEZONE_CHANGED);

                 registerReceiver(timeBroadcast,intentFilterTime);









    }




  public void TimeUpdate(){
      Calendar calendar=Calendar.getInstance();
      int hour=calendar.get(calendar.HOUR_OF_DAY);
      int minute=calendar.get(calendar.MINUTE);
      StringBuffer buffer=new StringBuffer();
      if (hour<10&&minute<10){
          buffer.append("0"+hour+":"+"0"+minute);
      }
      else if (hour<10&&minute>10){
          buffer.append("0"+hour+":"+minute);
      }
      else if (hour>10&&minute>10){
          buffer.append(hour+":"+minute);
      } else  if(hour>10&&minute<10){
          buffer.append(hour+":"+"0"+minute);
      }
      time_text.setText(buffer);
  }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(networkchangeReceiver);
        unregisterReceiver(timeBroadcast);

    }

    private void initBunder() {
               time_text=findViewById(R.id.time_text);
    }
}
